package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

/**
 * @author: libin
 * @date: 2019/11/23 10:07
 * @version: 1.0
 * @describe:
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        try {
            //把原始文件名拿出来
            String originalFilename = imgFile.getOriginalFilename();
            //获取.jpg
            int index = originalFilename.lastIndexOf(".");
            String s = originalFilename.substring(index);
            //生成随机名字
            String newFileName= UUID.randomUUID().toString()+s;
            //调用千云工具类
            QiniuUtils.upload(imgFile.getBytes(),newFileName);

            //调用redis把图片存储到第一个集合
            Jedis resource = jedisPool.getResource();
            resource.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,newFileName);
            resource.close();
            return new Result(true,"",newFileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }

    /**
     * 分页查询套餐
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.findAll(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );

        return pageResult;
    }

    /**
     * 添加套餐
     * @param setmeal
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal) {
        try {
            setmealService.add(setmeal);

            //调用redis,将图片存入第二个集合中
            Jedis resource = jedisPool.getResource();
            resource.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            resource.close();

            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

}
